package developer.zyk.global.upload;

import developer.zyk.global.common.consts.OssPlatformEnum;
import developer.zyk.global.config.UploadProperties;
import developer.zyk.global.upload.dao.entity.ChatObjectRepositoryDO;
import developer.zyk.global.upload.dao.mappers.ChatObjectRepositoryMapper;
import developer.zyk.global.upload.entity.UploadResult;
import developer.zyk.global.upload.upload.handlers.DoUploadRequest;
import developer.zyk.global.upload.upload.handlers.UploadHandlerResult;
import developer.zyk.global.upload.upload.handlers.UploadService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;

/**
 * @author zhangyongkang
 * @time 2025/5/15 19:28
 * @description
 */
@Component
public class FileOssTemplate {
    @Resource
    private Map<OssPlatformEnum, UploadService> uploadServiceMap;
    @Resource
    private UploadProperties uploadProperties;
    @Resource
    private ChatObjectRepositoryMapper chatObjectRepositoryMapper;


    public UploadResult uploadWithLocalFile() {
        return null;
    }

    public UploadResult uploadWithInputStream(InputStream inputStream,
                                              String path,
                                              String fileName
    ) {
        return uploadWithInputStream(
                inputStream,
                null, path, fileName
        );
    }

    public UploadResult uploadWithInputStream(InputStream inputStream,
                                              String bucketName,
                                              String path,
                                              String fileName
    ) {
        try (inputStream) {
            UploadService uploadService = uploadServiceMap.get(OssPlatformEnum.queryByCode(uploadProperties.getPlatFrom()));
            UploadHandlerResult uploadHandlerResult = uploadService.singleUploadStream(
                    DoUploadRequest.builder()
                            .fileName(fileName)
                            .path(path)
                            .bucketName(bucketName)
                            .build(),
                    inputStream
            );
            ChatObjectRepositoryDO chatObjectRepositoryDO = convertResult2DO(uploadHandlerResult, fileName);
            chatObjectRepositoryMapper.insert(chatObjectRepositoryDO);
            return UploadResult
                    .builder()
                    .fileId(chatObjectRepositoryDO.getId())
                    .url(uploadHandlerResult.getUrl())
                    .build();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public File download(Long fileId) {
        return uploadServiceMap.get(OssPlatformEnum.queryByCode(uploadProperties.getPlatFrom()))
                .download(fileId);
    }


    private ChatObjectRepositoryDO convertResult2DO(UploadHandlerResult uploadHandlerResult, String fileName) {
        String fileType = fileName.substring(Math.max(fileName.lastIndexOf("."), 0));

        OssPlatformEnum ossPlatformEnum = OssPlatformEnum.queryByCode(uploadProperties.getPlatFrom());
        ChatObjectRepositoryDO repositoryDO = new ChatObjectRepositoryDO();
        repositoryDO.setFileName(fileName);
        repositoryDO.setBucketName(uploadHandlerResult.getBucketName());
        repositoryDO.setFileType(fileType);
        repositoryDO.setUrl(uploadHandlerResult.getUrl());
        repositoryDO.setPath(uploadHandlerResult.getPath());
        repositoryDO.setHost(uploadHandlerResult.getHost());
        repositoryDO.setPlatformOutId(uploadHandlerResult.getPlatFormId());
        if (Objects.nonNull(ossPlatformEnum)) {
            repositoryDO.setPlatformCode(ossPlatformEnum.getCode());
            repositoryDO.setPlatformName(ossPlatformEnum.getName());
        }
        repositoryDO.setExtension("");
        repositoryDO.setCreateTime(LocalDateTime.now());
        repositoryDO.setCreateBy("");
        return repositoryDO;
    }
}
